// store index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use( Vuex )
// 初始化时用sessionStore.getItem('token'),这样子刷新页面就无需重新登录
const state={
    user: window.sessionStorage.getItem( 'user' ) ? JSON.parse( window.sessionStorage.getItem( 'user' ) ) : '',
    token: window.sessionStorage.getItem( 'token' ),
    tabbarShow: true
}
const getters={
    getUserInfo() {
        return state.user
    },
    getToken(){
        return state.token
    },
    getTabbarShow( state ) {
        return state.tabbarShow
    }
}
const mutations={
        // 将token保存到sessionStorage里，token表示登录状态
        SET_TOKEN: ( state, data ) => {
            state.token=data
            window.sessionStorage.setItem( 'token', data )
        },
        // 获取用户名
        SET_USER: ( state, data ) => {
            // 把用户名存起来
            state.user=data
            window.sessionStorage.setItem( 'user', JSON.stringify( data ) )
        },
        // 登出
        LOGOUT: ( state ) => {
            // 登出的时候要清除token
            state.token=null
            state.user=null
            window.sessionStorage.removeItem( 'token' )
            window.sessionStorage.removeItem( 'user' )
        },
        //tabbar方法
        updateTabbarShow( state, payload ) {
            state.tabbarShow=payload
        }
    }

const actions={}
export default new Vuex.Store( {
    state,
    getters,
    mutations,
    actions
} )